package com.shanyutiwen.springbootblogtest.controller;

import com.shanyutiwen.springbootblogtest.model.PostModel;
import com.shanyutiwen.springbootblogtest.response.AjaxResponse;
import com.shanyutiwen.springbootblogtest.service.PostServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/rest")
public class PostController {
    @Resource
    PostServiceImpl postService;

    @PostMapping("/posts")
    public AjaxResponse savePost(@RequestBody PostModel postModel) {
        postService.savePost(postModel);
        log.info("posts :" + postModel);
        return AjaxResponse.success();
    }

    @DeleteMapping("/posts/{id}")
    public AjaxResponse deletePostById(@PathVariable("id") Long id) {
        postService.deletePost(id);
        log.info("delete post by id :" + id);
        return AjaxResponse.success();
    }

    @PutMapping("/posts")
    public AjaxResponse updatePost(@RequestBody PostModel postModel) {
        postService.updatePost(postModel);
        log.info("update post " + postModel);
        return AjaxResponse.success();
    }

    @GetMapping("/posts/{id}")
    public AjaxResponse getPostById(@PathVariable("id") Long id) {
        PostModel post = postService.getPostById(id);
        log.info("get post by id :" + id + ", post result :" + post);
        return AjaxResponse.success(post);
    }

    @GetMapping("/posts")
    public AjaxResponse getAllPost() {
        List<PostModel> postModelList = postService.getAll();
        log.info("get all post :" + postModelList);

        return AjaxResponse.success(postModelList);
    }

    @DeleteMapping("/posts")
    public AjaxResponse deleteAllPost() {
        log.info("delete all post byebye!");
        postService.deleteAllPost();
        return AjaxResponse.success();
    }

}
